package com.zyplayer.dubbo.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.zyplayer.dubbo.service.AnnotateService;
import org.apache.curator.framework.CuratorFramework;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/test")
public class TestController {
	
	@Resource
	private CuratorFramework zkClient;
	@Reference(version = "${dubbo.service.version}")
	private AnnotateService annotateService;
	
	@RequestMapping("/get")
	public String get() {
		return annotateService.get();
	}
	
//	@RequestMapping("/list")
//	public String list() throws Exception {
//		List<String> strings = zkClient.getChildren().forPath("/dubbo");
//		ApplicationConfig bean = SpringContextUtil.getBean(ApplicationConfig.class);
//		RegistryConfig providerConfig = SpringContextUtil.getBean(RegistryConfig.class);
//		for (String string : strings) {
//
//			Object dubboService = this.getDubboService(string, providerConfig.getAddress(), bean.getName());
//
////			string = string.substring(string.lastIndexOf(".") + 1);
////			Object bean = SpringContextUtil.getBean(AnnotateService.class);
//			if (dubboService != null) {
//				System.out.println(string + " 类已获取到 " + dubboService.getClass());
//			} else {
//				System.out.println(string + " 类未获取到");
//			}
//		}
//
//		return JSON.toJSONString(strings);
//	}
//
//	public static <T> T getDubboService(String interfStr, String zookeeperUrl, String appName) {
//		try {
//			ApplicationConfig application = new ApplicationConfig();
//			application.setName(appName);
//			RegistryConfig registryConfig = new RegistryConfig();
//			registryConfig.setAddress(zookeeperUrl);
//			Class<?> interf = Class.forName(interfStr);
//			ReferenceConfig<T> rc = new ReferenceConfig<>();
//			rc.setApplication(application);
//			rc.setInterface(interf);
//			return rc.get();
//		} catch (Exception e) {
//			return null;
//		}
//	}
}
